package android.pot.com.smartcar.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.pot.com.smartcar.R;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * Created by zhaoshilu on 2017/7/24.
 */
@SuppressLint("ValidFragment")
public class CallDialogView extends DialogFragment implements View.OnClickListener{

    private Context context;

    private String title;

    private String tip;

    public CallDialogView(Context context,String title,String tip) {
        this.context = context;
        this.title = title;
        this.tip = tip;
    }
    public CallDialogView(Context context,String title) {
        this.context = context;
        this.title = title;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View v = inflater.inflate(R.layout.dialog_call, null);
        getDialog().getWindow().getAttributes().windowAnimations = R.style.dialog_up_down;
        initBody(v);
        addListener();
        return v;
    }


    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setGravity(Gravity.CENTER);
        WindowManager.LayoutParams params = getDialog().getWindow()
                .getAttributes();
        params.width = dm.widthPixels*3/4;
        params.height = params.height;

        getDialog().getWindow().setAttributes(params);

        // 背景透明
        getDialog().getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));
    }

    private void initBody(View v) {
        TextView tv_left = v.findViewById(R.id.tv_left);
        TextView tv_right = v.findViewById(R.id.tv_right);
        TextView tv_title = v.findViewById(R.id.tv_title);
        TextView tv_tip = v.findViewById(R.id.tv_tip);
        if (title.contains("抱歉")){
            v.findViewById(R.id.line).setVisibility(View.GONE);
            tv_left.setVisibility(View.GONE);
            tv_right.setText("好的");
        }else if(title.contains("退出")){
            tv_left.setText("否");
            tv_right.setText("是");
        }else if (!TextUtils.isEmpty(tip)){
            if (tip.contains("call")){
                tv_tip.setVisibility(View.GONE);
                tv_left.setText("取消");
                tv_right.setText("呼叫");
            }else{
                tv_tip.setVisibility(View.VISIBLE);
                tv_tip.setText(tip);
                tv_left.setText("取消");
                tv_right.setText("呼叫");
            }

        }else{
            v.findViewById(R.id.line).setVisibility(View.GONE);
            tv_left.setVisibility(View.GONE);
            tv_right.setText("好的");
        }

        tv_title.setText(title);
        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
    }

    private void addListener(){
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_left:
                dismiss();
                break;
            case R.id.tv_right:
                if (listener!=null) {
                    listener.methodClick();
                }
                dismiss();
                break;

        }
    }
    private OnMethodClickListener listener;

    public interface OnMethodClickListener {
        void methodClick();
    }

    public void setMethodClickListener(OnMethodClickListener listener) {
        this.listener = listener;
    }
}

